<template>
  <div class="bodyBox">
    <FrameStyle>
      <!-- 面包屑 -->
      <Breadcrumb :breadName="`贫困户管理`" slot="breadcrumb" />
      <!-- 搜索框 -->
      <el-input slot="search">
        <el-button class="el-icon-search" slot="append"></el-button>
      </el-input>
      <!-- 添加按钮 -->
      <el-button type="primary" slot="addBtn" @click="addDiaLogVisible = true"
        >添加贫困户</el-button
      >
      <!-- 表格列表 -->
      <el-table :data="tableData" stripe slot="tableList" height="370px">
        <el-table-column
          label="创建时间"
          prop="createTime"
          width="120px"
        ></el-table-column>
        <el-table-column prop="sn" label="编号" width="120px"></el-table-column>
        <el-table-column prop="name" label="姓名"></el-table-column>
        <el-table-column
          label="家庭人数"
          prop="number"
          width="120px"
        ></el-table-column>
        <el-table-column prop="income" label="人均收入"> </el-table-column>
        <el-table-column prop="cover" label="封面" width="150px">
          <template slot-scope="scope">
            <img :src="scope.row.cover" height="60px" />
          </template>
        </el-table-column>
        <el-table-column label="点击次数" prop="clickNum"> </el-table-column>
        <el-table-column prop="reply" label="是否审核"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="openPoorDiaLog(scope.row)"
              >修改</el-button
            >
            <el-button type="danger" size="mini" @click="delPoor(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <Pagination
        slot="pageSlot"
        :pageNo="pageNum"
        :pageSize="pageSize"
        :totalRows="totalRows"
        @pageChange="pageChange"
      />
    </FrameStyle>
    <!-- //添加弹出层 -->
    <el-dialog :visible.sync="addDiaLogVisible">
      <el-form :model="addForm">
        <el-form-item label="编号">
          <el-input v-model="addForm.sn"></el-input>
        </el-form-item>
        <el-form-item label="家庭成员">
          <el-input v-model="addForm.member"></el-input>
        </el-form-item>
        <el-form-item label="成员人数">
          <el-input v-model="addForm.number"></el-input>
        </el-form-item>
        <el-form-item label="家庭住址">
          <el-input v-model="addForm.address"></el-input>
        </el-form-item>
        <!-- <el-form-item label="家庭状况">
          <el-input v-model="addForm.condition"></el-input>
        </el-form-item> -->
        <el-form-item label="封面">
          <UploadImg :type="`POOR`" @getImgUrl="getImgUrl" />
        </el-form-item>
        <el-form-item label="人均收入">
          <el-input v-model="addForm.income"></el-input>
        </el-form-item>

        <el-form-item label="详细介绍">
          <el-input v-model="addForm.introduce"></el-input>
        </el-form-item>
        <el-form-item label="账号">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="是否审核">
          <el-input v-model="addForm.audit"></el-input>
        </el-form-item>
        <el-form-item label="是否回复">
          <el-input v-model="addForm.reply"></el-input>
        </el-form-item>
        <el-form-item label="点击次数">
          <el-input v-model="addForm.clickNum"></el-input>
        </el-form-item>
      </el-form>
      <div>
        <el-button type="primary" @click="addPoor">确认</el-button>
        <el-button>重置</el-button>
      </div>
    </el-dialog>
    <!-- 修改弹出层 -->
    <el-dialog :visible.sync="changeDiaVisible">
      <el-form :model="changeForm" ref="ruleForm">
        <el-form-item label="编号" prop="sn">
          <el-input v-model="changeForm.sn"></el-input>
        </el-form-item>
        <el-form-item label="家庭成员" prop="member">
          <el-input v-model="changeForm.member"></el-input>
        </el-form-item>
        <el-form-item label="成员人数" prop="number">
          <el-input v-model="changeForm.number"></el-input>
        </el-form-item>
        <el-form-item label="家庭住址" prop="address">
          <el-input v-model="changeForm.address"></el-input>
        </el-form-item>
        <!-- <el-form-item label="家庭状况">
          <el-input v-model="addForm.condition"></el-input>
        </el-form-item> -->
        <el-form-item label="封面" >
          <UploadImg :type="`POOR`" @getImgUrl="getImgUrl" />
        </el-form-item>
        <el-form-item label="人均收入" prop="income">
          <el-input v-model="changeForm.income"></el-input>
        </el-form-item>

        <el-form-item label="详细介绍" prop="introduce">
          <el-input v-model="changeForm.introduce"></el-input>
        </el-form-item>
        <el-form-item label="账号" prop="username">
          <el-input v-model="changeForm.username"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="changeForm.name"></el-input>
        </el-form-item>
        <el-form-item label="是否审核" prop="audit">
          <el-input v-model="changeForm.audit"></el-input>
        </el-form-item>
        <el-form-item label="是否回复" prop="reply">
          <el-input v-model="changeForm.reply"></el-input>
        </el-form-item>
      </el-form>
      <div>
        <el-button type="primary" @click="changePoor">确认</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  reqPinKunHuList,
  reqAddData,
  reqDelData,
  reqChangeData,
} from "../../../api";
export default {
  data() {
    return {
      tableData: [],
      pageNum: 1,
      pageSize: 10,
      totalRows: 1,
      addForm: {
        sn: "",
        member: "",
        number: "",
        address: "",
        cover: "",
        income: "",
        introduce: "",
        username: "",
        name: "",
        audit: "",
        reply: "",
        clickNum: "",
        lastClickTime: "",
      },
      imgUrl: "",
      changeForm: {},
      addDiaLogVisible: false,
      changeDiaVisible: false,
    };
  },
  created() {
    this.getData();
  },
  methods: {
    //获取贫困户列表数据
    async getData() {
      let { pageNum, pageSize } = this;
      let result = await reqPinKunHuList({ pageNum, pageSize });
      if (result.code != 200) return false;
      result.data.list.forEach((item) => {
        item.createTime = this.$format(item.createTime);
      });
      this.tableData = result.data.list;
      this.totalRows = result.data.total;
    },
    async addPoor() {
      this.addForm.cover = this.imgUrl;
      let result = await reqAddData("/poor/add", this.addForm);
      if (result.code != 200) return this.$message.error("添加失败");
      this.getData();
      this.$message({ type: "success", message: "添加成功" });
      this.addDiaLogVisible = false;
    },
    //删除贫困户
    async delPoor(id) {
      let result = await reqDelData("/poor/delete", id);
      if (result.code != 200) return this.$message.error("删除失败");
      this.getData();
      this.$message({ type: "success", message: "删除成功" });
    },
    //打开修改弹出层
    openPoorDiaLog(row) {
      this.changeDiaVisible = true;
      delete row.createTime
      this.$nextTick(() => {
        this.changeForm = row;
      });
    },
    //修改贫困户
    async changePoor() {
      this.imgUrl != "" ? (this.changeForm.cover = this.imgUrl) : false;
      let result = await reqChangeData("/poor/update", this.changeForm);
      if (result.code != 200) return this.$message.error("修改失败");
      this.getData();
      this.$message({ type: "success", message: "修改成功" });
      this.changeDiaVisible = false;
    },
    getImgUrl(url) {
      this.imgUrl = url;
    },
    //重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //分页器事件
    pageChange(page) {
      this.pageNum = page;
      this.getData();
    },
  },
};
</script>